package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.ButcherReportSurveyRecordDao;
import com.jiantong.work.model.ButcherReportSurveyRecord;
import com.jiantong.work.service.ButcherReportSurveyRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/23.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ButcherReportSurveyRecordReadServiceImpl implements ButcherReportSurveyRecordReadService{

    private final ButcherReportSurveyRecordDao butcherReportSurveyRecordDao;

    @Autowired
    public ButcherReportSurveyRecordReadServiceImpl(ButcherReportSurveyRecordDao butcherReportSurveyRecordDao) {
        this.butcherReportSurveyRecordDao = butcherReportSurveyRecordDao;
    }

    @Override
    public Response<ButcherReportSurveyRecord> findByReport(Long reportId) {
        try{
            ButcherReportSurveyRecord butcherReportSurveyRecord = butcherReportSurveyRecordDao.findByReport(reportId);
            if(butcherReportSurveyRecord == null){
                log.error("butcherReportSurveyRecord(reportId={}) is not found", reportId);
                return Response.fail("butcherReportSurveyRecord.not.found");
            }
            return Response.ok(butcherReportSurveyRecord);
        }catch (Exception e){
            log.error("failed to find butcherReportSurveyRecord(reportId={}), cause:{}", reportId, Throwables.getStackTraceAsString(e));
            return Response.fail("butcherReportSurveyRecord.find.fail");
        }
    }

    @Override
    public Long findIdByReportId(Long reportId) {
        try{
           return butcherReportSurveyRecordDao.findIdByReportId(reportId);
        }catch (Exception e){
            //todo
            return null;
        }
    }

    /**
     * 根据Id查找
     *
     * @param id
     */
    @Override
    public Response<ButcherReportSurveyRecord> findById(Long id) {
        try {
            ButcherReportSurveyRecord butcherReportSurveyRecord = butcherReportSurveyRecordDao.findById(id);
            if (butcherReportSurveyRecord == null) {
                log.error("butcherReportSurveyRecord(id={}) is not found", id);
                return Response.fail("butcherReportSurveyRecord.not.found");
            }
            return Response.ok(butcherReportSurveyRecord);
        }catch (Exception e){
            log.error("failed to find butcherReportSurveyRecord(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("butcherReportSurveyRecord.find.fail");
        }
    }

    @Override
    public Response<List<ButcherReportSurveyRecord>> findByNameOrNumber(Map criteria) {
        try {
            return Response.ok(butcherReportSurveyRecordDao.findByNameOrNumber(criteria));
        } catch (Exception e) {
            log.error("failed to find breedReportSurveyRecord id(reportId = {}),cause{}", Throwables.getStackTraceAsString(e));
            return null;
        }
    }
}
